<?php

namespace app\admin\model;

use think\Db;
use think\Model;
use think\Validate;

class CustomerInfoModel extends Model
{

    public function add($data){
        $validate = new Validate($this->rule,$this->msg);
        $result = ['status' => true, 'msg' => '保存成功' , 'data' => ''];
        if(!$validate->check($data))
        {
            $result['status'] = false;
            $result['msg'] = $validate->getError();
        } else {
            if (!$this->allowField(true)->save($data)) {
                $result['status'] = false;
                $result['msg'] = '保存失败';
            }

        }
        return $result;
    }
    public function isMobile($value)
    {
        $rule = '^1(3|4|5|7|8|9)[0-9]\d{8}$^';
        $result = preg_match($rule, $value);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    public static function getmuzheng($czr_id){
        $field='ci.buyer_name,ci.cart_type,ci.customer_account,ci.buyer_phone,ci.customer_address,p.tomb_num,p.tomb_region,p.tomb_address,p.layer_num,p.tomb_bit_number
        ,o.sign_time,o.tomb_typei';
        $order_info =  Db::name('order')->alias('o')->field($field)
            ->join("dj_tombarea_row_col c","o.tomb_id = c.colID")
            ->join("dj_customer_info ci","o.customer_id = ci.id")
            ->join("dj_tombarea_information_product p","o.tomb_product_id = p.id")
            ->where(['o.customer_id'=>$czr_id])->find();

        $czr_info['czr_name'] = $order_info['buyer_name'];
        $czr_info['cart_type'] = cart_type('cart',$order_info['cart_type']);
        $czr_info['czr_cart_no'] = $order_info['customer_account'];
        $czr_info['czr_sex'] = get_sex($order_info['customer_account']);
        $czr_info['czr_dh'] = $order_info['buyer_phone'];
        $czr_info['czr_zz'] = $order_info['customer_address'];
        $czr_info['num'] = $order_info['tomb_num'];
        $czr_info['wz'] =  $order_info['tomb_region']. $order_info['tomb_address'].$order_info['layer_num'].$order_info['tomb_bit_number'];
        $czr_info['sg'] = unixtime_to_date($order_info['sign_time'],"Y.m.d");
        if ($order_info['tomb_typei']==1){
            $shizhe_info = ArchivesDeceasedModel::where(['customer_id' => $czr_id])->find();
            $dep_info = OrderDepositModel::where(['customer_id'=>$czr_id])->find();
            $sz_info['id'] = $shizhe_info['deceased_id'];
            $sz_info['name'] = $shizhe_info['deceased_name'];
            $sz_info['nr'] = $shizhe_info['convergence_type'];
            $sz_info['az_nian'] = unixtime_to_date(($dep_info['deposit_time']), "Y");
            $sz_info['az_yue'] = unixtime_to_date(($dep_info['deposit_time']), "m");
            $sz_info['az_ri'] = unixtime_to_date(($dep_info['deposit_time']), "d");
            $sz_info['end_az_nian'] = unixtime_to_date(($dep_info['deposit_end_time']), "Y");
            $sz_info['end_az_yue'] = unixtime_to_date(($dep_info['deposit_end_time']), "m");
            $sz_info['end_az_ri'] = unixtime_to_date(($dep_info['deposit_end_time']), "d");
            $sz_info['zheng'] = true;
            if ($shizhe_info['convergence_type'] == 1) {
                $sz_info['nr'] = '骨灰';
            } else {
                $sz_info['nr'] = '骨殖';
            }
            if ($shizhe_info['gender'] == 1) {
                $sz_info['sex'] = '男';
            } else {
                $sz_info['sex'] = '女';
            }
            $sz[] = $sz_info;
        }else {
            $shizhe_list = ArchivesDeceasedModel::where(['customer_id' => $czr_id])->select();
            $sz = [];
            foreach ($shizhe_list as $key => &$value) {
                $sz_info['id'] = $value['deceased_id'];
                $sz_info['name'] = $value['deceased_name'];
                $sz_info['nr'] = $value['convergence_type'];
                $sz_info['az_nian'] = unixtime_to_date(date_to_unixtime($value['funeral_time']), "Y");
                $sz_info['az_yue'] = unixtime_to_date(date_to_unixtime($value['funeral_time']), "m");
                $sz_info['az_ri'] = unixtime_to_date(date_to_unixtime($value['funeral_time']), "d");
                $sz_info['zheng'] = true;
                if ($value['convergence_type'] == 1) {
                    $sz_info['nr'] = '骨灰';
                } else {
                    $sz_info['nr'] = '骨殖';
                }
                if ($value['gender'] == 1) {
                    $sz_info['sex'] = '男';
                } else {
                    $sz_info['sex'] = '女';
                }
                $sz[] = $sz_info;
            }
        }

        $result_data['czr'] = $czr_info;//持证人
        $result_data['sz'] = $sz;//逝者
        $result_data['fzjg'] = '';//发证机构
        $result_data['gz'] = '';//
        $result_data['nian'] = unixtime_to_date(time(),"Y");//
        $result_data['yue'] = unixtime_to_date(time(),"m");//
        $result_data['ri'] = unixtime_to_date(time(),"d");//

//        exit(json_encode($shizhe_list));

        return $result_data;
    }

}